//
//  InfoManageController.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/9/3.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "InfoManageController.h"
#import "UIAlertController+category.h"
#import "ConfirmRealNameController.h"
#import "ChangeNickNameController.h"
@interface InfoManageController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UIButton *changeHeadButton;
@property (weak, nonatomic) IBOutlet UILabel *nickNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;

@end

@implementation InfoManageController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
- (void)setupUI{
    [self.changeHeadButton setImageEdgeInsets:UIEdgeInsetsMake(0.0, -10, 0.0, 0.0)];
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:self.model.img] placeholderImage:[UIImage imageNamed:@"默认头像"]];
    self.nickNameLabel.text = self.model.nickname;
    self.phoneLabel.text = self.model.phone;
    if ([self.model.isRealName isEqualToString:@"0"]) {
        self.statusLabel.text = @"未认证";
    } else {
        self.statusLabel.text = @"已认证";;
    }
}
- (IBAction)backButtonClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)changeHeadButtonClick:(UIButton *)sender {
    UIImagePickerController *pick = [[UIImagePickerController alloc]init];
    pick.allowsEditing = YES;
    pick.delegate = self;
    [UIAlertController showPhotoAlerControllerPicker:pick controller:self sourceView:nil];
}
// 实名认证
- (IBAction)realNameButtonClick:(UIButton *)sender {
    ConfirmRealNameController * vc = [[ConfirmRealNameController alloc] init];
    vc.model = self.model;
    [self.navigationController pushViewController:vc animated:YES];
}
// 修改昵称
- (IBAction)changeNickNameButtonClick:(UIButton *)sender {
    ChangeNickNameController * vc = [[ChangeNickNameController alloc] init];
    vc.changeSuccessfulClick = ^(NSString * _Nonnull nickNameStr) {
        self.nickNameLabel.text = nickNameStr;
    };
    [self presentViewController:vc animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *image = info[UIImagePickerControllerEditedImage];
    
    [self upLoadHeadIcon:image];
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)upLoadHeadIcon:(UIImage *)image {
    [MBProgressHUD showHUDAddedTo:self.view msg:@"请稍后..." animated:YES];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [NetWorkTool UploadPicWithUrl:uploadImg param:param key:@"headImg" image:image withSuccess:^(id dic) {
        NSString *imgURLStr = dic[@"data"];
        NSMutableDictionary *param2 = [NSMutableDictionary dictionary];
        param2[@"userId"] = USER_ID;
        param2[@"img"] = imgURLStr;
      //  param[@"nickname"] = @"";
        
        [NetWorkTool POST:updateUser param:param2 success:^(id dic) {
            ShowToastWithText(@"上传成功");
            self.headImageView.image = image;
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        } other:^(id dic) {
            ShowToastWithText(@"上传失败请重试!");
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        } fail:^(NSError *error) {
            ShowToastWithText(@"上传失败请重试!");
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        } needUser:YES];
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}
@end
